/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mika
 */
public class MatrixReader {

    ArrayList<Integer> list;
    ArrayList<int[][]> matrices;

    public MatrixReader() {
        list = new ArrayList();
    }

    private ArrayList<Integer> readFromFile(File path) {
        ArrayList<Integer> list = new ArrayList();
        FileReader fr = null;
        try {
            fr = new FileReader(path);
            BufferedReader br = new BufferedReader(fr);
            String s;
            while ((s = br.readLine()) != null) {
                list.add(new Integer(s));
            }
            //System.out.println("Size: " + list.size());
            //System.out.println("sqrt(Size):" + (Math.sqrt(list.size())));



            fr.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MatrixReader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MatrixReader.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fr.close();
            } catch (IOException ex) {
                Logger.getLogger(MatrixReader.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return list;

    }

    private int[][] createSquareMatrix(ArrayList<Integer> list) {
        double check = Math.sqrt(list.size());
        int[][] returnValue = null;

        if (check == Math.floor(check)) {
            int lineSize = (int) check;
            returnValue = new int[lineSize][lineSize];
            int n = 0;
            for (int i = 0; i < lineSize; i++) {
                for (int j = 0; j < lineSize; j++) {
                    //System.out.println("i=" + i + "|j=" + j);
                    returnValue[i][j] = list.get(n);
                    n++;
                }
            }
        } else {
            System.out.println("Not Square!");
            returnValue = null;
        }
        return returnValue;

    }

    public int[][] makeMatrix(File path) {
        ArrayList<Integer> list = readFromFile(path);
        return createSquareMatrix(list);

    }
    
    public int[][] getNextMatrix() {
        return matrices.remove(0);
    }

    public void readAllMatrices(int matrixNo) {
        this.matrices = new ArrayList();
        int[][] m;
        for (int i = 0; i < matrixNo; i++) {
            m = makeMatrix(new File("./matrix" + i));
            this.matrices.add(m);
        }

        
    }

    //not tested
    public ArrayList<int[][]> readAllMatrices(String path, int matrixNo) {
        matrices = new ArrayList();
        int[][] m;
        for (int i = 0; i < matrixNo; i++) {
            m = makeMatrix(new File(path+"/matrix" + i));
            matrices.add(m);
        }

        return matrices;
    }
    
    public ArrayList<int[][]> getMatrices() {
        return matrices;
    }

    int getSize() {
        return this.matrices.size();
    }
}
